package com.study.demo.gsbean.demo1;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource;

import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author: elvin
 * @time: 2019-04-16 14:38
 * @desc:
 **/
public class Start {

    public static void main(String[] args) {
        String path = "demo1/application.xml";
        //ClassPathXmlApplicationContext bf = new ClassPathXmlApplicationContext(path);

        Resource resource = new ClassPathResource(path);
        BeanFactory bf = new XmlBeanFactory(resource);

        //Man man = (Man) bf.getBean("man");
        Man man = (Man) bf.getBean("man");
        man.walk();

       /* Man d = (Man) ((XmlBeanFactory) bf).getSingleton("a");
        if(d == null){
            System.out.printf("nothing ... ");
        }
        else{
            d.walk();
        }*/

        //factoryBean(bf);
    }

    private static void factoryBean(BeanFactory bf) {
      /*  Man man = (Man) bf.getBean("myFactoryBean");
        man.walk();*/

        MyFactoryBean factoryBean = (MyFactoryBean) bf.getBean("&myFactoryBean");
        try {
            Man man1 = factoryBean.getObject();
            man1.walk();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void alias(BeanFactory bf) {
        Man man = (Man) bf.getBean("d");

        String[] aliases = bf.getAliases("a");
        for (String alias : aliases) {
            System.out.println(alias);
        }
        System.out.println("------1------");
        boolean has = ((XmlBeanFactory) bf).hasAlias("man", "c");
        System.out.println(has);

        System.out.println("-------2-----");
        ((XmlBeanFactory) bf).registerAlias("e", "f");
        aliases = bf.getAliases("e");
        for (String alias : aliases) {
            System.out.println(alias);
        }
        System.out.println("---------3---");
        String a = ((XmlBeanFactory) bf).canonicalName("a");
        System.out.println(a);
    }

    private static void aware(BeanFactory bf, String path) {
        MyBeanFactoryAware myBeanFactoryAware = (MyBeanFactoryAware) bf.getBean("myBeanFactoryAware");
        myBeanFactoryAware.walk();

        System.out.println("------------");
        MyBeanNameAware myBeanNameAware = (MyBeanNameAware) bf.getBean(MyBeanNameAware.class);
        myBeanNameAware.printfName();

        System.out.println("========");
        MyBeanClassLoaderAware myBeanClassLoaderAware = (MyBeanClassLoaderAware) bf.getBean("myBeanClassLoaderAware");
        myBeanClassLoaderAware.printResource(path);
    }

    private static void printFile(Resource resource) {
        System.out.println(((ClassPathResource) resource).getPath());
        try {
            InputStream inputStream = resource.getInputStream();
            int length = inputStream.available();
            byte[] bytes = new byte[length];
            int read = inputStream.read(bytes, 0, length);
            String str = new String(bytes);
            System.out.println(str);
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }

    /*public static void main(String[] args){
        ApplicationContext ac = new FileSystemXmlApplicationContext("classpath:demo1/application.xml");
        Person person = (Person)ac.getBean("person");
        person.walk();
    }*/


}
